//
//  ISBaseController.m
//  iSocket
//
//  Created by lhj on 1/23/16.
//  Copyright © 2016 boer. All rights reserved.
//

#import "IMBaseController.h"
#import "IMHud.h"

@interface IMBaseController ()

@property (weak, nonatomic) UITapGestureRecognizer *tap;

@end

@implementation IMBaseController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.isAppear = YES;
    [self.view endEditing:YES];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.isAppear = NO;
    
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
//    ISLog(@"%@",NSStringFromClass([self class]));
}

// 是否处于显示
- (BOOL)isVisible{

    return self.isViewLoaded && self.view.window;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
}


#pragma mark - 方法

// 转圈
- (void)showHud{
    [self showStatus:nil];
}

// 叹号加信息
- (void)showInfo:(NSString *)info{
    
    [self addHudGesture];
    [IMHud showInfoWithStatus:info];
}

// 转圈加信息
- (void)showStatus:(NSString *)status{
    
    [self addHudGesture];
    [IMHud showWithStatus:status];
}

// 勾加信息
- (void)showSuccess:(NSString *)success{
    
    [self addHudGesture];
    [IMHud showSuccessWithStatus:success];
}

// 叉加信息
- (void)showError:(NSString *)errorMsg{
    [self addHudGesture];
    [IMHud showErrorWithStatus:errorMsg];
}

- (void)dismissHud{
    [IMHud dismiss];
    if (self.tap) {
        [self.view removeGestureRecognizer:self.tap];
    }
}

- (void)addHudGesture{
    [SVProgressHUD setViewForExtension:self.view];
    [IMHud setContainerView:self.view];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissHud)];
    self.tap = tap;
    [self.view addGestureRecognizer:tap];
}

- (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message cancelTitle:(NSString *)cancelTitle confirmTitle:(NSString *)confirmTitle confirm:(void (^)())confirm{
    if (!title) {
        title = @"温馨提示";
    }
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    if (cancelTitle) {
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action1];
    }
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:confirmTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        !confirm ? : confirm();
    }];
    
    [alert addAction:action2];
    [self presentViewController:alert animated:YES completion:nil];
}

@end
